<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script src="./lib/vue.js"></script>
</body>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./lib/vue.js"></script>
</head>

<body>
  <div id="app">
    <!-- <input type="text" v-phone="setPhoneMsg" v-model="phone"> -->
    <!-- <input type="text" v-phone="{fn:setPhoneMsg}" v-model="phone"> -->
    <!-- 修饰符 -->
    <input type="text" v-validate.phone="{fn:setPhoneMsg}" v-model="phone">
    <!-- <input type="text" v-validate.email="{fn:setPhoneMsg}" v-model="phone"> -->
    <span>{{phone_msg}}</span>
  </div>

  <script>

    // Vue.directive('phone', function (el, { value: cb = () => { } }) {
    // Vue.directive('phone', function (el, { value }) {
    //   value('123')
    // })

    // Vue.directive('phone', {
    //   update(el, { value }) {
    //     value('手机号码不合法')
    //   }
    // })

    // Vue.directive('phone', {
    //   update(el, { value: { fn, rule = null } }) {
    //     console.log(fn);
    //     fn('手机号码不合法')
    //   }
    // })

    Vue.directive('validate', {
      update(el, { value, modifiers ,arg}) {
        console.log(value.fn)
        console.log(modifiers)
        if (modifiers.phone) {
          value.fn('手机号码不合法')
        } else if (modifiers.email) {
          value.fn('邮箱不合法')
        }
      }
    })

    let vm = new Vue({
      el: '#app',
      data: {
        phone: '',
        phone_msg: '请输入有效的手机号码'
      },
      methods: {
        setPhoneMsg(v) {
          this.phone_msg = v
        }
      }
    })
  </script>
</body>

</html>